<template>
  <div class="app-container">
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-upload
                :action="apiUrl + '/upload/'"
                multiple
                :on-success="getList"
            >
              <el-button type="primary">上传</el-button>
            </el-upload>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table highlight-current-row border v-loading="loading" :data="regFileList"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="编号" align="center" prop="doc_id"/>
        <el-table-column label="文件名" show-overflow-tooltip align="center" prop="doc_name"/>
        <el-table-column label="文件类型" align="center" prop="type"/>
        <el-table-column label="文件大小" align="center" prop="fileLength"/>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            {{ scope.row.status == 1 ? '正常' : '失效' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-link
                class="ml-1"
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
            >删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          class="mt-2"
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改注册文件对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="SysRegFileRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="文件大小" prop="fileLength">
          <el-input v-model="form.fileLength" placeholder="请输入文件大小"/>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysRegFile">
import {ref, reactive, nextTick, computed, toRefs, onMounted, getCurrentInstance} from 'vue'
import {post, get} from '@/utils/request'
import {apiUrl} from '@/config'

const {proxy} = getCurrentInstance()

const regFileList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fileName: null,
    fileLength: null,
    status: null,
    creator: null,
    operator: null,
  },
  rules: {}
})

const {queryParams, form, rules} = toRefs(data)

/** 查询注册文件列表 */
function getList() {
  loading.value = true
  const param = {}
  param.page_size = 10
  param.current_page = 1
  post('/retrieve/get_documents_paging', param, 'json').then(r => {
    console.log(r)
    regFileList.value = r.rows
    total.value = r.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    fileName: null,
    fileLength: null,
    status: 0,
    creator: null,
    createTime: null,
    operator: null,
    updateTime: null
  }
  proxy.$refs.Ref?.resetFields()('SysRegFileRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.$refs.Ref?.resetFields()('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加注册文件'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  /*querySysRegFile(id).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改注册文件'
  })*/
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['SysRegFileRef'].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        /*updateSysRegFile(form.value).then(response => {
          proxy.$modal.success('修改成功')
          open.value = false
          getList()
        })*/
      } else {
        /*addSysRegFile(form.value).then(response => {
          proxy.$modal.success('新增成功')
          open.value = false
          getList()
        })*/
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除注册文件编号为"' + ids + '"的数据项？').then(function () {
    // return delSysRegFile(ids)
  }).then(() => {
    getList()
    proxy.$modal.success('删除成功')
  }).catch(() => {
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sys/SysRegFile/export', {
    ...queryParams.value
  }, `SysRegFile_${new Date().getTime()}.xlsx`)
}

getList()
</script>
